/**
 * 
 */
package test;

import java.util.List;

import org.apache.log4j.Logger;

import test.hibernate.DAOFactory;
import test.hibernate.dao.HibernateUtil;

/**
 * @author Sinisa
 *
 */
public class ObjectPersister {
        
        private static final Logger LOG = Logger.getLogger(ObjectPersister.class);

        public void save(CompanyInfo companyInfo) {
                HibernateUtil.getSessionFactory().getCurrentSession()
                .beginTransaction();
                
                List<CompanyInfo> company = DAOFactory.DEFAULT.buildRequestorDefinitionDAO().findByUrl(companyInfo.getUrl());
                if (company.size() == 1) {
                        companyInfo.setId(company.get(0).getId());
                        DAOFactory.DEFAULT.buildRequestorDefinitionDAO().update(companyInfo);
                } else if (company.size() > 1) {
                        LOG.warn("There should be only one record with url: "+companyInfo.getUrl()+" but there are: "+company.size());
                } else {
                        DAOFactory.DEFAULT.buildRequestorDefinitionDAO().save(companyInfo);
                }
                HibernateUtil.getSessionFactory().getCurrentSession().getTransaction()
                .commit();
        }
        
}
